#include "LoginManager.h"

CLoginManager* CLoginManager::s_ptr = NULL;

CLoginManager::CLoginManager(mysqlpp::Connection* pConn)
{
	this->m_pCon = pConn;
	this->s_ptr = this;
	Init();
}


CLoginManager::~CLoginManager()
{


}


void CLoginManager::Init()
{
	if(this->m_pCon == NULL)
		return;
	mysqlpp::Query query = m_pCon->query("select * from cq_user");

	mysqlpp::StoreQueryResult res = query.store();
	if (res)
	{
		cout<<endl<<"*******************************"<<endl;
		cout<<"USER size is "<<endl;
		cout<< res.size() ;
	 
		for (size_t i = 0; i < res.num_rows(); ++i)
        	{
			LOGIN_INFO loginInfo;
            		strcpy(loginInfo.USER_NAME, res[i]["name"]); //次序
			strcpy(loginInfo.PWD, res[i]["password"]);
			cout<<loginInfo.USER_NAME<<" : "<<loginInfo.PWD<<endl;
			this->m_vUserInfo.push_back(loginInfo);
			cout<<"*************************"<<endl;
		}
	}
	else
	{
		cout<<"eorror read data"<<endl;
	}
}

bool CLoginManager::CheckUserLogin(char* userName, char* password)
{
	vector<LOGIN_INFO>::iterator itr = this->m_vUserInfo.begin();
	while(itr != this->m_vUserInfo.end())
	{
		if(strncmp(userName, (*itr).USER_NAME,(strlen((*itr).USER_NAME)-1)))
		{
			int ret = strncmp(password, (*itr).PWD,(strlen((*itr).PWD)-1));
			if (ret == 0)
				return true;
			else
				return false;
		}
		itr++;	
	}

	return false;


}


CLoginManager* CLoginManager::GetInstence()
{
	return s_ptr;
}

bool CLoginManager::AddNewUser(int fd_sock)
{

CUser* pUser = new CUser(fd_sock);
 return true;
}

